var infoList = [
    new ConfigBean(0, "公司名称", 4, true, "name"),
    new ConfigBean(0, "公司地址", 4, true, "address"),
    new ConfigBean(0, "详细地址", 4, true, "realAddress"),
    new ConfigBean(0, "成立时间", 5, true, "createDate")
];

function pickerInit(name, data) {
    $(".select-" + name).on("click", function () {
        var picker = new mui.PopPicker();
        picker.setData(data);
        $("input[name=" + name + "]").val() && picker.pickers[0].setSelectedValue($("input[name=" + name + "]").val());
        picker.show(function (selectItems) {
            $(".select-" + name).html(selectItems[0].text).addClass("selected");
            $("input[name=" + name + "]").val(selectItems[0].value);
        });
    });
}

function dtPickerInit(name) {
    $(".date-" + name).on("click", function () {
        var picker = new mui.DtPicker({
                type: 'date',
                beginYear: 1970,
                endDate: new Date()
            }
        );
        $("input[name=" + name + "]").val() && picker.setSelectedValue($("input[name=" + name + "]").val());
        picker.show(function (selectItems) {
            $(".date-" + name).html(selectItems.value).addClass("selected");
            $("input[name=" + name + "]").val(selectItems.value);
        });
    });
}

(function () {
    window.intervalStep = 100;
    window.intervalTime = 0;
    window.intervalSecond = 1.9;
    var interval = null;
    var dot = ['.', '..', '...', '....', '.....', '......'];
    window.showMatching = function () {
        $('.img').css({
            "background": "url('../../../img/percent/waiting.gif') center no-repeat",
            "background-size": "contain"
        });
        interval = setInterval(function () {
            intervalTime++;
            $('.img div').html('匹配中<br/>' + dot[intervalTime % 6]);
        }, intervalStep);
        $('.matching').css('display', 'flex');
    };

    window.hideMatching = function () {
        $('.img').css({
            "background": "url('') center no-repeat",
            "background-size": "contain"
        });
        intervalTime = 0;
        $('.matching').hide();
        clearInterval(interval);
    };
})();

function adapter() {
    var flag = false;
    var configs = [];
    var id = $.paramToObj().id;
    var data = id ? {policyId: id} : {};
    $(".info input").each(function (event) {
        if (flag) return;
        var $this = $(this);
        if ($.trim($this.val()) === "") {
            if (!flag) {
                flag = "请填写" + $this.attr("data-name") + "！";
            }
        } else {
            data[$this.attr("name")] = $this.val();
        }
    });
    if (flag) {
        toast(flag);
        return false;
    }
    $(".require input").each(function (event) {
        if (flag) return;
        var $this = $(this);
        if ($.trim($this.val()) === "") {
            if (!flag) {
                flag = "请填写" + $this.attr("data-name") + "！";
            }
        } else {
            configs.push(new ConfigSubmitBean($this.attr("name"), $this.attr("data-id"), $this.attr("data-type"), $this.val()));
        }
    });
    if (flag) {
        toast(flag);
        return false;
    }
    $(".info,.require").find("input[type=number]").each(function (event) {
        if (flag) return;
        var $this = $(this);
        if ($.trim($this.val()).length > 10) {
            if (!flag) {
                flag = $this.attr("data-name") + "不能超过10位！";
            }
        }
    });
    if (flag) {
        toast(flag);
        return false;
    }
    $(".optional input").each(function (event) {
        var $this = $(this);
        configs.push(new ConfigSubmitBean($this.attr("name"), $this.attr("data-id"), $this.attr("data-type"), $this.val()));
    });
    data.configs = configs;
    showMatching();
    ajax({
        url: "/api/match/submit",
        type: "post",
        dataType: "json",
        headers: {
            "content-type": "application/json"
        },
        data: JSON.stringify(data),
        success: function (result) {
            debugger;
            switch (result.code) {
                case 0:
                case "0": {
                    if (intervalTime * intervalStep < intervalSecond * 1000) {
                        setTimeout(function () {
                            openWebview('home-result');
                            hideMatching();
                        }, intervalSecond * 1000 - intervalTime * intervalStep);
                    } else {
                        openWebview('home-result');
                        hideMatching();
                    }
                    break;
                }
                default: {
                    hideMatching();
                    toast(result.msg);
                }
            }
        },
        complete: function () {
        },
        waiting: false
    });
}

function onPageReady() {
    if (!getLocalItem('user')) {
        tokenExpires();
    } else {
        ajax({
            url: "/api/match/allConfig",
            type: "post",
            dataType: "json",
            headers: {},
            data: {userId: new AppUserBean(getLocalItem("user")) ? new AppUserBean(getLocalItem("user")).id : ""},
            success: function (result) {
                $("body").render("#main", {});
                for (i in infoList) {
                    var item = infoList[i];
                    item.js_show = '';
                    switch (infoList[i].type) {//(notes = "1||数值类型，2:boolean类型，3:下拉选，4:文本类型，5:日期类型")
                        case 1:
                        case "1": {
                            $(".info").render("#number", item);
                            break;
                        }
                        case 4:
                        case "4": {
                            $(".info").render("#text", item);
                            break;
                        }
                        case 5:
                        case "5": {
                            if (result.data[infoList[i].entryKey]) {
                                item.configValue = result.data[infoList[i].entryKey];
                                $(".info").render("#dated", item);
                            } else {
                                $(".info").render("#date", item);
                            }
                            dtPickerInit(item.entryKey);
                            break;
                        }
                        default: {
                            toast(item.name + "配置类型错误");
                        }
                    }
                    $(".info input[name=" + infoList[i].entryKey + "]").val(result.data[infoList[i].entryKey] || "");
                }
                for (var i in result.data.configs) {
                    item = new ConfigBean(result.data.configs[i]);
                    switch (item.type) {//(notes = "1||数值类型，2:boolean类型，3:下拉选，4:文本类型")
                        case 1:
                        case "1": {
                            $("." + item.isRequired).render("#number", item, "append");
                            break;
                        }
                        case 2:
                        case "2":
                        case 3:
                        case "3": {
                            $("." + item.isRequired).render("#" + item.select, item, "append");
                            pickerInit(item.entryKey, item.values);
                            break;
                        }
                        case 4:
                        case "4": {
                            $("." + item.isRequired).render("#text", item, "append");
                            break;
                        }
                        default: {
                            toast(item.name + "配置类型错误");
                        }
                    }
                }
                $('.__js_show__').remove();
                $(".help").on("click", function () {
                    alert($.trim($(this).parent().next('.remark').text()));
                });
                $(".footer").render("#footer");
            }
        });
    }
}

function more() {
    return false;
    $('.more').hide();
    $('.optional').show();
    $('.require :nth-last-child(1)').css('margin-bottom', '0');
}